package com.ki.rent.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ki.rent.entity.Host;
import com.ki.rent.entity.House;
import com.ki.rent.entity.User;
import com.ki.rent.service.HostService;
import com.ki.rent.service.HouseService;
import com.ki.rent.service.UserService;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author
 */
@Controller
@RequestMapping("/rent/host")
public class HostController {

    @Autowired
    private HostService hostService;
    @Autowired
    private UserService userService;
    @Autowired
    private HouseService houseService;

    @RequestMapping("/go")
    public String go(){
        return "html/customer";
    }

    @RequestMapping("/signOutHost")
    public String signOut(Model model, HttpServletRequest request){
        User user = (User) request.getSession().getAttribute("loginUser");
        Integer userId = user.getUserId();
        boolean b = hostService.removeById(userId);
        QueryWrapper<House> wrapper = new QueryWrapper<>();
        wrapper.eq("host_id",userId);
        boolean re = houseService.remove(wrapper);
        model.addAttribute("user",user);
        if (b && re){
            model.addAttribute("msg","重新登录以进入用户界面");
            return "html/hostSign";
        }
        model.addAttribute("msg","修改出错");
        return "html/hostSign";
    }

}

